
$(function() {
	
	_change("input[id^='name_s']:visible", "select[id^='name_t']:visible");
	
	$("input[id^='name_s']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'finance.php?do=MoneyMy.ajax_zjm',
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							label: item.name + " [" + item.zjm + "]",
							value: item.name
						}
					}));
				}
			});
		},
		
		minLength: 1
	});
	
	$("input[id^='amount']:visible").css("width", 85);
	
	$("select[id^='bank_s']:visible").multiselect({
		minWidth: 140,
		selectedList: 4
	}).multiselect("uncheckAll");
	
	$("select[id^='currency_s']:visible").multiselect({
		minWidth: 140,
		selectedList: 4
	}).multiselect("uncheckAll");
	
	$("select[id^='category_s']:visible").multiselect({
		minWidth: 140,
		selectedList: 4
	}).multiselect("uncheckAll");
});

var MoneyMy = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {	
			"关闭": function() { 
				$(this).dialog("close");
			},			
			
			"添加": function() {
				var form = $("#form_add")[0];
				
				if (form.currency.value == '-') {
					alert("请选择币种！");
					return false;
				}
				
				if (!confirm("确认是否添加？")) {
					return false;
				}
	
				var body = { };
				body.emp = form.emp.value;
				
				body.bank = form.bank.value;
				body.name = form.name.value;
				body.type = form.type.value;
				body.category = form.category.value;

				body.currency = form.currency.value;
				body.amount = form.amount.value;
				body.rate = form.rate.value;
				body.rmb = form.rmb.value;
				
				body.content = form.content.value;
									
				$(this).dialog("close");
	
				$.post('finance.php?do=MoneyMy.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'finance.php?do=MoneyMy.edit&id=' + id;
		var c2 = 'finance.php?do=MoneyMy.view&id=' + id;
		var c3 = 'finance.php?do=MoneyMy.copy&id=' + id;
		
		var a1 = ['编辑信息', 'url', 'MoneyMy.dialog_edit_', '650x420'];	
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x420'];
		var a3 = ['复制信息', 'url', 'MoneyMy.dialog_copy_', '650x400'];
		
		var s = c1 + ", " + c2 + ", " + c3;

		return this.popup_(e, [a1, a2, a3], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var ss = 'finance.php?do=MoneyMy.ss&id=' + id;
		var bb = ['修改状态', 'url', 'MoneyMy.dialog_ss', '400x200'];
		
		s += ", " + ss;
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		result = Common.result();
		$.getJSON('finance.php?do=MoneyMy.audit&item=145', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'finance.php?do=MoneyMy.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {			
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"提交": function() {
				var form = $("#form_edit")[0];
				
				if (form.currency.value == '-') {
					alert("请选择币种！");
					return false;
				}
				
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var body = { };
				body.id = form.id.value;
				body.emp = form.emp.value;
				
				body.bank = form.bank.value;
				body.name = form.name.value;
				body.type = form.type.value;
				body.category = form.category.value;
				
				body.date = form.date.value;
				body.currency = form.currency.value;
				body.amount = form.amount.value;
				body.rate = form.rate.value;
				body.rmb = form.rmb.value;
				
				body.date = form.date.value;
				body.content = form.content.value;
				body.status = form.status.value;
										
				$(this).dialog("close");
	
				var url = 'finance.php?do=MoneyMy.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_edit(url, title, xy);
	},
	
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {			
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"复制": function() {
				var form = $("#form_edit")[0];
				
				if (form.currency.value == '-') {
					alert("请选择币种！");
					return false;
				}
				
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var body = { };
				body.emp = form.emp.value;
				
				body.bank = form.bank.value;
				body.name = form.name.value;
				body.type = form.type.value;
				body.category = form.category.value;
				
				body.currency = form.currency.value;
				body.amount = form.amount.value;
				body.rate = form.rate.value;
				body.rmb = form.rmb.value;
				
				body.content = form.content.value;
										
				$(this).dialog("close");
	
				var url = 'finance.php?do=MoneyMy.addPost';
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_edit(url, title, xy);
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'finance.php?do=MoneyMy.delete' + p2;	
		var c2 = 'finance.php?do=MoneyMy.batch_tj';
		var c3 = 'finance.php?do=MoneyMy.xls&' + p2;		
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];
		
		var a2 = ['汇总统计', 'url', 'MoneyMy.dialog_batch_tj', '800x450'];
		var a3 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c2;

		return popup(e, [a1, a2], s);
	},	
	
	dialog_batch_tj: function (url, title, xy) {
		var result = Common.result();		
		var c = new Array();
		$("input[name^='c_']").each(function() {
			if ($(this).attr("checked")) {
				var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
				c.push(s);
			}
		});
		
		var body = c.join("&");
		
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			}
		});

		var view = '#dialog_edit';
		$.get(url, body, function(data) {
			$(view).html(data);
		});

		var regex = /^(\d+)(x)(\d+)$/;
		if (regex.test(xy)) {
			var cc = xy.split("x");
			var width = parseInt(cc[0]);
			var height = parseInt(cc[1]);
			$(view).dialog('option', 'height', height);
		} else {
			var width = parseInt(xy);
		}

		$(view).dialog('option', 'title', title);
		$(view).dialog('option', 'width', width);
		$(view).dialog('open');
	}
};
